//
//  TouTiaoVC.m
//  BaseProject
//
//  Created by hundredsandc on 2017/10/30.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "TouTiaoVC.h"
#import "RecommendTableViewCell.h"
#import "HTTPManagers.h"
#import "WEBViewController.h"
@interface TouTiaoVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArr;

@end

@implementation TouTiaoVC
{
    NSInteger page;
    NSInteger type;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    page = 1;
    
    if (_index ==1001) {
        self.title = @"头条";
        type = 1;
        [self getNbaFootBall];
    }else if (_index==1002){
        self.title = @"NBA";
        type =17;
        [self getNbaFootBall];
    }else{
        self.title = @"足球";
        type =12;
        [self getNbaFootBall];
    }
    [self.view addSubview:self.tableView];
    
}
-(void)getNbaFootBall{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"channel_id"]=@(type);
    params[@"access_token"] = @"";
    params[@"page_index"] = @(page);
    params[@"page_size"]= @"20";
    params[@"note_type"] = @"10";
    
    [HTTPManagers postNBAFootBallWithURL:@"new_notes_info_query" params:params success:^(id json) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        NSDictionary *dict = [json objectForKey:@"new_notes_info_query"];
        NSArray *arr = dict[@"data"];
        for (int i = 0; i<arr.count; i++) {
            NSDictionary *dict = arr[i];
            [self.dataArr addObject:dict];
        }
        
        [_tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
-(void)getData{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"page_size"]=@"20";
    params[@"page_index"] = @(page);
    params[@"info_type"] = @(type);
    params[@"app_version"]= @"1.2.7";
    params[@"channel_no"] = @"360";
    
    
    [HTTPManagers getWithRecommendURL:@"qry_infos" params:params success:^(id json) {
        if ([[json objectForKey:@"result_msg"]isEqualToString:@"成功"]) {
            NSDictionary *infos = [json objectForKey:@"qry_infos"];
            NSArray *arr= [infos objectForKey:@"data"];
            if (self.dataArr.count>0) {
                [_dataArr removeAllObjects];
            }
            self.dataArr = [arr mutableCopy];
            [_tableView reloadData];
        }
        
        
    } failure:^(NSError *error) {
        
    }];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dict = self.dataArr[indexPath.section];
    WEBViewController *web = [WEBViewController new];
    if (type==0) {
    web.url = dict[@"info_url"];
    }else{
        web.url = dict[@"share_url"];
    }
    [self.navigationController pushViewController:web animated:YES];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([RecommendTableViewCell class]) forIndexPath:indexPath];
    if (type==0) {
        if (self.dataArr.count>0) {
            NSDictionary *dict = self.dataArr[indexPath.section];
            [cell customLeftImageUrlStr:dict[@"info_pic"] type:dict[@"info_type_name"] title:dict[@"info_title"] time:dict[@"info_time"]];
        }
    }else{
        if (self.dataArr.count>0) {
            NSDictionary *dict = self.dataArr[indexPath.section];
            [cell customLeftImageUrlStr:dict[@"share_pic"] type:dict[@"channel_name"] title:dict[@"note_title"] time:dict[@"crt_time"]];
        }
    }
    
    return cell;
    
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([RecommendTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([RecommendTableViewCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            if (self.dataArr.count>0) {
                [self.dataArr removeAllObjects];
            }
            [self getNbaFootBall];
            
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            page++;
            [self getNbaFootBall];
            
        }];
    }
    return _tableView;
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}


@end
